#include "StdAfx.h"

Panel::Panel(ui32 x, ui32 y, std::string szTitle) : m_title("Fonts\\FrizQT__.ttf", "Friz Quadrata TT", 7, 15)
{
	m_hoveredText = -1;
	m_visible = true;
	m_closeHovered = false;
	m_moveAble = false;
	m_fadedIn = false;
	m_fadeIn = false;
	m_fadeStart = 0;
	m_szTitle = szTitle;
	m_position.x = (float)x;
	m_position.y = (float)y;
	m_size.x = 190;
	m_size.y = 250;
	m_frameTex = CTextureHandler::GetInstance()->GetTexture("Interface\\LootFrame\\UI-LootPanel.blp");
	m_closeIcon = CTextureHandler::GetInstance()->GetTexture("Interface\\Buttons\\UI-Panel-MinimizeButton-Up.blp");
	m_closeIconDown = CTextureHandler::GetInstance()->GetTexture("Interface\\Buttons\\UI-Panel-MinimizeButton-Down.blp");
	m_circleIcon = CTextureHandler::GetInstance()->GetTexture("Interface\\TimeManager\\GlobeIcon.blp");
	
	float cx = 190;
	float cy = 250;

	m_frameVer[0].z = m_frameVer[1].z = m_frameVer[2].z = m_frameVer[3].z = 0.0f;
	m_frameVer[0].rhw = m_frameVer[1].rhw = m_frameVer[2].rhw = m_frameVer[3].rhw = 1.0f;
	m_frameVer[0].x = (float)x;
	m_frameVer[0].y = (float)y;
	m_frameVer[0].u = 0.0f;
	m_frameVer[0].v = 0.0f;

	for(ui32 i = 0; i < 4; ++i)
	{
		m_frameVer[i].dwcolor = m_closeVertices[i].dwcolor = m_iconVertices[i].dwcolor = 0xFFFFFFFF;
	}

	m_frameVer[1].x = (float)x + cx;
	m_frameVer[1].y = (float)y;
	m_frameVer[1].u = 190.0f / 255.0f;
	m_frameVer[1].v = 0.0f;

	m_frameVer[2].x = (float)x + cx;
	m_frameVer[2].y = (float)y + cy;
	m_frameVer[2].u = 190.0f / 255.0f;
	m_frameVer[2].v = 252.0f / 255.0f;

	m_frameVer[3].x = (float)x;
	m_frameVer[3].y = (float)y + cy;
	m_frameVer[3].u = 0.0f;
	m_frameVer[3].v = 252.0f / 255.0f;

	m_closeVertices[0].z = m_closeVertices[1].z = m_closeVertices[2].z = m_closeVertices[3].z = 0.0f;
	m_closeVertices[0].rhw = m_closeVertices[1].rhw = m_closeVertices[2].rhw = m_closeVertices[3].rhw = 1.0f;
	m_closeVertices[0].x = (float)(x + 161);
	m_closeVertices[0].y = (float)(y + 9);
	m_closeVertices[0].u = 0.0f;
	m_closeVertices[0].v = 0.0f;

	m_closeVertices[1].x = (float)(x + 161 + 32);
	m_closeVertices[1].y = (float)(y + 9);
	m_closeVertices[1].u = 1;
	m_closeVertices[1].v = 0.0f;

	m_closeVertices[2].x = (float)(x + 161 + 32);
	m_closeVertices[2].y = (float)(y + 9 + 32);
	m_closeVertices[2].u = 1;
	m_closeVertices[2].v = 1;

	m_closeVertices[3].x = (float)(x + 161);
	m_closeVertices[3].y = (float)(y + 9 + 32);
	m_closeVertices[3].u = 0.0f;
	m_closeVertices[3].v = 1;

	m_iconVertices[0].z = m_iconVertices[1].z = m_iconVertices[2].z = m_iconVertices[3].z = 0.0f;
	m_iconVertices[0].rhw = m_iconVertices[1].rhw = m_iconVertices[2].rhw = m_iconVertices[3].rhw = 1.0f;
	m_iconVertices[0].x = (float)x + 8;
	m_iconVertices[0].y = (float)y + 6;
	m_iconVertices[0].u = 0.0f;
	m_iconVertices[0].v = 0.0f;

	m_iconVertices[1].x = (float)x + 8 + 64;
	m_iconVertices[1].y = (float)y + 6;
	m_iconVertices[1].u = 1.0f;
	m_iconVertices[1].v = 0.0f;

	m_iconVertices[2].x = (float)x + 8 + 64;
	m_iconVertices[2].y = (float)y + 6 + 64;
	m_iconVertices[2].u = 1.0f;
	m_iconVertices[2].v = 1.0f;

	m_iconVertices[3].x = (float)x + 8;
	m_iconVertices[3].y = (float)y + 6 + 64;
	m_iconVertices[3].u = 0.0f;
	m_iconVertices[3].v = 1.0f;
}

void Panel::Draw()
{
	if(!m_visible)
		return;
	ui32 texAlpha = 255;
	if(m_fadeStart)
	{
		if(m_fadeIn)
		{
			ui32 alpha = ui32(((GetTickCount() - m_fadeStart) * (255.0f / 1000.0f)));
			if(alpha >= 255)
			{
				m_fadedIn = true;
				m_fadeStart = 0;
				alpha = 255;
				for(ui32 i = 0; i < 4; ++i)
					m_frameVer[i].dwcolor = m_closeVertices[i].dwcolor = m_iconVertices[i].dwcolor = 0xFFFFFFFF;
			}
			else
			{
				texAlpha = alpha;
				for(ui32 i = 0; i < 4; ++i)
				{
					m_frameVer[i].dwcolor &= ~ui32(0xFF000000);
					m_frameVer[i].dwcolor |= (alpha << 24);
					m_closeVertices[i].dwcolor &= ~ui32(0xFF000000);
					m_closeVertices[i].dwcolor |= (alpha << 24);
					m_iconVertices[i].dwcolor &= ~ui32(0xFF000000);
					m_iconVertices[i].dwcolor |= (alpha << 24);
				}
			}
		}
		else
		{
			i32 alpha = i32(255 - ((GetTickCount() - m_fadeStart) * (255.0f / 1000.0f)));
			if(alpha <= 0)
			{
				m_fadedIn = false;
				m_fadeStart = 0;
				alpha = 0;
				m_visible = false;
				for(ui32 i = 0; i < 4; ++i)
					m_frameVer[i].dwcolor = m_closeVertices[i].dwcolor = m_iconVertices[i].dwcolor = 0x00FFFFFF;
			}
			else
			{
				texAlpha = alpha;
				for(ui32 i = 0; i < 4; ++i)
				{
					m_frameVer[i].dwcolor &= ~ui32(0xFF000000);
					m_frameVer[i].dwcolor |= (alpha << 24);
					m_closeVertices[i].dwcolor &= ~ui32(0xFF000000);
					m_closeVertices[i].dwcolor |= (alpha << 24);
					m_iconVertices[i].dwcolor &= ~ui32(0xFF000000);
					m_iconVertices[i].dwcolor |= (alpha << 24);
				}
			}
		}
	}
	sD3D->SetFVF(VERTEX3FCRHWT);
	sD3D->SetTexture(0, *m_circleIcon);
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_iconVertices, sizeof(Vertex3FCRHWT));
	sD3D->SetTexture(0, *m_frameTex);
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_frameVer, sizeof(Vertex3FCRHWT));
	if(!m_closeHovered)
		sD3D->SetTexture(0, *m_closeIcon);
	else
		sD3D->SetTexture(0, *m_closeIconDown);
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_closeVertices, sizeof(Vertex3FCRHWT));
	if(m_szTitle != "")
		m_title.DrawTextA(ui32(117.0f - (m_szTitle.length() / 2.0f * m_title.GetFontWidth()) + m_position.x), ui32(m_position.y + 17), 90, 20, m_szTitle, 0x00E5B200 | (texAlpha << 24)); 
	std::vector<PanelText>::iterator itr = m_texts.begin();
	std::vector<PanelText>::iterator end = m_texts.end();

	for(ui32 i = 0; itr != end; ++itr, ++i)
	{
		if(m_hoveredText != -1 && i == m_hoveredText)
			m_title.DrawTextA(ui32(m_position.x + 30), ui32(m_position.y + 80 + (i * m_title.GetFontHeight() + 2)), 130, 20, (*itr).m_text, (0x00E5B200) | (texAlpha << 24));
		else
			m_title.DrawTextA(ui32(m_position.x + 30), ui32(m_position.y + 80 + (i * m_title.GetFontHeight() + 2)), 130, 20, (*itr).m_text, (0x00FFFFFF) | (texAlpha << 24));
	}
}

void Panel::MouseActivity(bool left, Vertex2F pos, bool up)
{
	if(!m_visible)
		return;
	if(pos.x - m_position.x >= 165 && pos.x - m_position.x <= (161 + 25) && pos.y - m_position.y >= 15 && pos.y - m_position.y <= (9 + 25) && m_fadedIn)
	{
		if(m_closeHovered && up)
			m_visible = false;
		m_closeHovered = !up;
		return;
	}
	else if(up && m_fadedIn)
	{
		if(m_closeHovered)
			m_visible = false;
		m_moveAble = false;
	}
	else if(pos.x - m_position.x >= 70 && pos.x - m_position.x <= 163 && pos.y - m_position.y >= 14 && pos.y - m_position.y <= 33)
	{
		m_moveLast = pos;
		m_moveAble = true;
	}
	else if(m_hoveredText >= 0 && m_hoveredText < (i32)m_texts.size())
	{
		sChat.AddText(m_texts[m_hoveredText].m_info);
	}
}

void Panel::OnMouseMove(ui32 x, ui32 y, bool lDown, bool rDown)
{
	if(!m_visible)
		return;
	if(m_moveAble)
	{
		m_position.x += x - m_moveLast.x;
		m_position.y += y - m_moveLast.y;
		for(ui32 i = 0; i < 4; ++i)
		{
			m_frameVer[i].x += x - m_moveLast.x;
			m_frameVer[i].y += y - m_moveLast.y;
			m_closeVertices[i].x += x - m_moveLast.x;
			m_closeVertices[i].y += y - m_moveLast.y;
			m_iconVertices[i].x += x - m_moveLast.x;
			m_iconVertices[i].y += y - m_moveLast.y;
		}
		m_moveLast.x = (float)x;
		m_moveLast.y = (float)y;
	}
	else
	{
		if(m_visible)
		{
			if(x >= m_position.x + 30 && x <= m_position.x + 160 && y >= m_position.y + 80 && y <= (m_position.y + 80 + m_texts.size() * m_title.GetFontHeight() + 2))
			{
				if(!sTutorial.TutorialShown(TUTORIAL_PANEL_HOVER))
					sTutorial.ShowTutorial("Panels", helpPanel);
				ui32 m_rest = y - ui32((m_position.y + 80));
				m_hoveredText = (ui32)floor((float)m_rest / (m_title.GetFontHeight()));
			}
			else
				m_hoveredText = -1;
		}
	}
}

void Panel::FadeIn()
{
	m_visible = true;
	m_fadeStart = GetTickCount();
	m_fadedIn = false;
	m_fadeIn = true;
}

void Panel::FadeOut()
{
	m_fadeStart = GetTickCount();
	m_fadedIn = false;
	m_fadeIn = false;
}